/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Models.FeedBack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Admin
 */
@ManagedBean
@RequestScoped
public class FeedbackDAO {

    public int insertFeedBack(FeedBack fb) {
        int result = 0;
        try {
            Connection con = DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("insert into Feedback values(?,?,?,?,?,?)");
            stm.setString(1, fb.getName());
            stm.setString(2, fb.getMobile());
            stm.setString(3, fb.getEmail());
            stm.setString(4, fb.getCity());
            stm.setString(5, fb.getType());
            stm.setString(6, fb.getDescreption());

            //4.Thu thi truy van
            stm.executeUpdate();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public ArrayList<FeedBack> getAllFeedback() {
        ArrayList<FeedBack> listFeedback = new ArrayList<FeedBack>();
        try {
            Connection con = DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("select * from Feedback");
            ResultSet rs = stm.executeQuery();

            while (rs.next()) {
                FeedBack f = new FeedBack();
                f.setId(rs.getInt("ID"));
                f.setName(rs.getString("Name"));
                f.setMobile(rs.getString("Mobile"));
                f.setEmail(rs.getString("Email"));
                f.setCity(rs.getString("City"));
                f.setType(rs.getString("Type"));
                f.setDescreption(rs.getString("Description"));
                                
                listFeedback.add(f);
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listFeedback;
    }
    
    public void deleteFeedbackByID(int id){
        try {
            Connection con = DBConnection.myConnection();
            PreparedStatement stm = con.prepareStatement("DELETE FROM Feedback WHERE ID = ?");
            stm.setInt(1, id);
            stm.executeUpdate();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public int statisticFeed(){
        int total = 0;
        try{
            Connection con = DBConnection.myConnection();
            PreparedStatement st = con.prepareStatement("Select count(*) from Feedback");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                total = rs.getInt(1);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
       return total;
    }
}
